﻿using System.Globalization;
using dpa.Library.Helper;
using dpa.Library.Models;
using Xceed.Document.NET;
using Xceed.Words.NET;

namespace dpa.Library.Services;

public class DocxService : IDocxService
{
    [Obsolete("Obsolete")]
    public Task<(bool, string)> GenerateReport(AirportInfo airportInfo)
    {
        try
        {
            // 加载模板文件
            string templatePath = Environment.CurrentDirectory + @"\WordTemplete\template.docx";
            
            using DocX document = DocX.Load(templatePath);
            
            document.ReplaceText("{{Date}}", airportInfo.Date);
            document.ReplaceText("{{AirportName}}", airportInfo.AirportName);

            string outputPath = @$"C:\Users\Public\Desktop\{airportInfo.Date}{airportInfo.AirportName}机场航班正常性情况报告.docx";

            //1
            document.ReplaceText("{{FlightsReleased.Count}}", airportInfo.FlightsReleased.Count.ToString());
            document.ReplaceText("{{FlightsReleased.Rate}}", airportInfo.FlightsReleased.Rate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{FlightsReleased.YearOnYearRate}}", airportInfo.FlightsReleased.YearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{FlightsReleased.MonthOnMonthRate}}", airportInfo.FlightsReleased.MonthOnMonthRate.AddUpOrDown());
            
            //2
            document.ReplaceText("{{AirportInitialFlight.Count}}", airportInfo.AirportInitialFlight.Count.ToString());
            document.ReplaceText("{{AirportInitialFlight.Rate}}", airportInfo.AirportInitialFlight.Rate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirportInitialFlight.YearOnYearRate}}", airportInfo.AirportInitialFlight.YearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{AirportInitialFlight.MonthOnMonthRate}}", airportInfo.AirportInitialFlight.MonthOnMonthRate.AddUpOrDown());
            
            //3
            document.ReplaceText("{{AirportInitialAllFlight.Rate}}", airportInfo.AirportInitialAllFlight.Rate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirportInitialAllFlight.MonthOnMonthRate}}", airportInfo.AirportInitialAllFlight.MonthOnMonthRate.AddUpOrDown());
            document.ReplaceText("{{AirportInitialAllFlight.YearOnYearRate}}", airportInfo.AirportInitialAllFlight.YearOnYearRate.AddUpOrDown());
        
            //4
            document.ReplaceText("{{AirportBridgingRate2.TotalRate}}", airportInfo.AirportBridgingRate2.TotalRate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirportBridgingRate2.MonthOnMonthRate}}", airportInfo.AirportBridgingRate2.MonthOnMonthRate.AddUpOrDown());
            document.ReplaceText("{{AirportBridgingRate2.YearOnYearRate}}", airportInfo.AirportBridgingRate2.YearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{AirportBridgingRate2.ArrivePortRate}}", airportInfo.AirportBridgingRate2.ArrivePortRate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirportBridgingRate2.LeavePortRate}}", airportInfo.AirportBridgingRate2.LeavePortRate.ToString(CultureInfo.CurrentCulture));
            
            //5
            document.ReplaceText("{{AirlineFlights.TotalCount}}", airportInfo.AirlineFlights.TotalCount.ToString());
            document.ReplaceText("{{AirlineFlights.TotalRate}}", airportInfo.AirlineFlights.TotalRate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirlineFlights.YearOnYearRate}}", airportInfo.AirlineFlights.YearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{AirlineFlights.MonthOnMonthRate}}", airportInfo.AirlineFlights.MonthOnMonthRate.AddUpOrDown());
            
            //6
            document.ReplaceText("{{AirlineTakesOff.TotalCount}}", airportInfo.AirlineTakesOff.TotalCount.ToString());
            document.ReplaceText("{{AirlineTakesOff.TotalRate}}", airportInfo.AirlineTakesOff.TotalRate.ToString(CultureInfo.InvariantCulture));
            document.ReplaceText("{{AirlineTakesOff.YearOnYearRate}}", airportInfo.AirlineTakesOff.YearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{AirlineTakesOff.MonthOnMonthRate}}", airportInfo.AirlineTakesOff.MonthOnMonthRate.AddUpOrDown());
            
            //7
            document.ReplaceText("{{AirlineDelay.AverageDelay}}", airportInfo.AirlineDelay.AverageDelay.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirlineDelay.YearOnYearRate}}", airportInfo.AirlineDelay.YearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{AirlineDelay.MonthOnMonthRate}}", airportInfo.AirlineDelay.MonthOnMonthRate.AddUpOrDown());
            document.ReplaceText("{{AirlineDelay.LessThan30Min}}", airportInfo.AirlineDelay.LessThan30Min.ToString());
            document.ReplaceText("{{AirlineDelay.LessThan30MinRate}}", airportInfo.AirlineDelay.LessThan30MinRate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirlineDelay.Between30MinTo1H}}", airportInfo.AirlineDelay.Between30MinTo1H.ToString());
            document.ReplaceText("{{AirlineDelay.Between30MinTo1HRate}}", airportInfo.AirlineDelay.Between30MinTo1HRate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirlineDelay.Between1HTo2H}}", airportInfo.AirlineDelay.Between1HTo2H.ToString());
            document.ReplaceText("{{AirlineDelay.Between1HTo2HRate}}", airportInfo.AirlineDelay.Between1HTo2HRate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirlineDelay.Between2HTo4H}}", airportInfo.AirlineDelay.Between2HTo4H.ToString());
            document.ReplaceText("{{AirlineDelay.Between2HTo4HRate}}", airportInfo.AirlineDelay.Between2HTo4HRate.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirlineDelay.MoreThan4H}}", airportInfo.AirlineDelay.MoreThan4H.ToString());
            document.ReplaceText("{{AirlineDelay.MoreThan4HRate}}", airportInfo.AirlineDelay.MoreThan4HRate.ToString(CultureInfo.CurrentCulture));

            var airlineDelay_guohang = airportInfo.AirlineDelays.First(a => a.AirlinCompany == "国航");
            document.ReplaceText("{{airlineDelay_guohang.LessThan30Min}}", airlineDelay_guohang.LessThan30Min.ToString());
            document.ReplaceText("{{airlineDelay_guohang.Between30MinTo1H}}", airlineDelay_guohang.Between30MinTo1H.ToString());
            document.ReplaceText("{{airlineDelay_guohang.Between1HTo2H}}", airlineDelay_guohang.Between1HTo2H.ToString());
            document.ReplaceText("{{airlineDelay_guohang.Between2HTo4H}}", airlineDelay_guohang.Between2HTo4H.ToString());
            document.ReplaceText("{{airlineDelay_guohang.MoreThan4H}}", airlineDelay_guohang.MoreThan4H.ToString());

            var airlineDelay_tianjinhang = airportInfo.AirlineDelays.First(a => a.AirlinCompany == "天津航");
            document.ReplaceText("{{airlineDelay_tianjinhang.LessThan30Min}}", airlineDelay_tianjinhang.LessThan30Min.ToString());
            document.ReplaceText("{{airlineDelay_tianjinhang.Between30MinTo1H}}", airlineDelay_tianjinhang.Between30MinTo1H.ToString());
            document.ReplaceText("{{airlineDelay_tianjinhang.Between1HTo2H}}", airlineDelay_tianjinhang.Between1HTo2H.ToString());
            document.ReplaceText("{{airlineDelay_tianjinhang.Between2HTo4H}}", airlineDelay_tianjinhang.Between2HTo4H.ToString());
            document.ReplaceText("{{airlineDelay_tianjinhang.MoreThan4H}}", airlineDelay_tianjinhang.MoreThan4H.ToString());

            var airlineDelay_huaxiahang = airportInfo.AirlineDelays.First(a => a.AirlinCompany == "华夏航");
            document.ReplaceText("{{airlineDelay_huaxiahang.LessThan30Min}}", airlineDelay_huaxiahang.LessThan30Min.ToString());
            document.ReplaceText("{{airlineDelay_huaxiahang.Between30MinTo1H}}", airlineDelay_huaxiahang.Between30MinTo1H.ToString());
            document.ReplaceText("{{airlineDelay_huaxiahang.Between1HTo2H}}", airlineDelay_huaxiahang.Between1HTo2H.ToString());
            document.ReplaceText("{{airlineDelay_huaxiahang.Between2HTo4H}}", airlineDelay_huaxiahang.Between2HTo4H.ToString());
            document.ReplaceText("{{airlineDelay_huaxiahang.MoreThan4H}}", airlineDelay_huaxiahang.MoreThan4H.ToString());

            
            var airlineDelay_tianjiaohang = airportInfo.AirlineDelays.First(a => a.AirlinCompany == "天骄航");
            document.ReplaceText("{{airlineDelay_tianjiaohang.LessThan30Min}}", airlineDelay_tianjiaohang.LessThan30Min.ToString());
            document.ReplaceText("{{airlineDelay_tianjiaohang.Between30MinTo1H}}", airlineDelay_tianjiaohang.Between30MinTo1H.ToString());
            document.ReplaceText("{{airlineDelay_tianjiaohang.Between1HTo2H}}", airlineDelay_tianjiaohang.Between1HTo2H.ToString());
            document.ReplaceText("{{airlineDelay_tianjiaohang.Between2HTo4H}}", airlineDelay_tianjiaohang.Between2HTo4H.ToString());
            document.ReplaceText("{{airlineDelay_tianjiaohang.MoreThan4H}}", airlineDelay_tianjiaohang.MoreThan4H.ToString());

            //8
            document.ReplaceText("{{AirportTaxiingExtraSlidingOutIn.OutAverageTime}}", airportInfo.AirportTaxiingExtraSlidingOutIn.OutAverageTime.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirportTaxiingExtraSlidingOutIn.OutYearOnYearRate}}", airportInfo.AirportTaxiingExtraSlidingOutIn.OutYearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{AirportTaxiingExtraSlidingOutIn.OutMonthOnMonthRate}}", airportInfo.AirportTaxiingExtraSlidingOutIn.OutMonthOnMonthRate.AddUpOrDown());
            document.ReplaceText("{{AirportTaxiingExtraSlidingOutIn.InAverageTime}}", airportInfo.AirportTaxiingExtraSlidingOutIn.InAverageTime.ToString(CultureInfo.CurrentCulture));
            document.ReplaceText("{{AirportTaxiingExtraSlidingOutIn.InYearOnYearRate}}", airportInfo.AirportTaxiingExtraSlidingOutIn.InYearOnYearRate.AddUpOrDown());
            document.ReplaceText("{{AirportTaxiingExtraSlidingOutIn.InMonthOnMonthRate}}", airportInfo.AirportTaxiingExtraSlidingOutIn.InMonthOnMonthRate.AddUpOrDown());

            
            // 保存为新文件
            document.SaveAs(outputPath);

            return Task.FromResult((true, outputPath));
        
        }
        catch (Exception e)
        {
            return Task.FromResult((false, "文件出现问题不完整或文件损坏"));
        }
    }

}